/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import util.Data_variante;
import util.user_info;

/**
 *
 * @author dragonboy
 */


public class add_user extends HttpServlet {
    String [] User_Type= {"","Member","Admin","SuperAdmin"};
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void show_user_info2(PrintWriter out, int currUserType , String currUserName) throws CorruptIndexException, IOException, ParseException{
        File file = new File(Data_variante.loginDB_file);
        FSDirectory dir = FSDirectory.open(file);
        Searcher searcher
            = new IndexSearcher(dir);
        QueryParser parser = new QueryParser(Version.LUCENE_34, "UserID", new StandardAnalyzer(Version.LUCENE_34));
        //String command = "Username:"+username+" AND Status:"+passwd;
        String command = "Username:" + currUserName + "OR Boss:" + currUserName;
        Query myquery = parser.parse(command);
        TopDocs results = searcher.search(myquery,50);
        ScoreDoc[] hits = results.scoreDocs;
        int numUser = 0;
        for(ScoreDoc hit: hits){
            Document d = searcher.doc(hit.doc);
            numUser++;
            out.println("<tr>"
                        +"<td class=\"table-the-first\">"+ numUser +"</td>"
                        +"<td>"+ d.get("Username") +"</td>"
                        +"<td class=\"content-action-table-comment\">"+ d.get("Displayname") +"</td>"
                        +"<td>"+ User_Type[Integer.valueOf(d.get("Type"))] +"</td>"
                        +"<td>"+ d.get("LastLogin") +"</td>"
                        +"</tr>");
        }
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, CorruptIndexException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(false);
        if (session == null){
            response.sendRedirect("../index.html");
            return;
        }
        int currUserType = Integer.valueOf((String) session.getAttribute("userType"));
        String currUserName = (String)session.getAttribute("username");
        try {
            out.println("<div id=\"content-comment\">"
                    + "<div id=\"content-header\">" + "Admin Box &gt; <b>Add User</b>" + "</div>"
                    + "<div id=\"content-body\">"
                    + "<div id=\"content-form\">"
                    + "<div id=\"header-tip\" style=\"color: red; font-size: 13px; margin-left: 200px;\"></div>"
                    + "<form name=\"AddRemoveUser\" action=\"add_remove_user\" method=\"POST\" onsubmit=\"xmlhttpPost(\'add_remove_user\', \'AddRemoveUser\', \'content-action\', \'\'); return false;\">"
                    //+"<form name=\"HanderContentComment\" action=\"submit_action\" method=\"POST\" onsubmit=\"xmlhttpPost(\'submit_action\', \'HanderContentComment\', \'content-action\', \'\'); return false;\">"
                    + "<table id=\"comment-table\" cellspacing=\"10\">"
                    + "<tbody>");
            if(currUserType > 1){
                out.println("<tr class=\"row\">"
                        +"<td class=\"col1\">User Type(*)</td>"
                        +"<td class=\"col2\">"
                        +"<select name=\"user_type\" class=\"form-content-select\">");
                out.println("<option value=\"Member\" selected=\"selected\">Member</option>");
                String _disable;
                if(currUserType > 2){
                    _disable = "";
                }else{
                    _disable = "DISABLED";
                }
                out.println("<option value=\"Admin\""+ _disable +">Admin</option>");
                if(currUserType > 3){
                    _disable = "";
                }else{
                    _disable = "DISABLED";
                }
                out.println("<option value=\"SuperAdmin\""+ _disable +">SuperAdmin</option>");
                out.println("<tr class'\"row\">"
                        +"<td class=\"col1\">Username(*)</td>"
                        +"<td class=\"col2\">"
                        +"<input type=\"text\" name=\"username\" value=\"\" class=\"form-content-input\">"
                        +"</tr>");
                out.println("<tr class'\"row\">"
                        +"<td class=\"col1\">Display name</td>"
                        +"<td class=\"col2\">"
                        +"<input type=\"text\" name=\"displayname\" value=\"\" class=\"form-content-input\">"
                        +"</tr>");
                out.println("<tr class'\"row\">"
                        +"<td class=\"col1\">Password(*)</td>"
                        +"<td class=\"col2\">"
                        +"<input type=\"password\" name=\"password\" value=\"\" class=\"form-content-input\">"
                        +"</tr>");
                out.println("<tr class'\"row\">"
                        +"<td class=\"col1\">Retype Password(*)</td>"
                        +"<td class=\"col2\">"
                        +"<input type=\"password\" name=\"retypepassword\" value=\"\" class=\"form-content-input\">"
                        +"</tr>");
                out.println("<tr class=\"row\">"
                        +"<td class=\"col1\"></td>"
                        +"<td class=\"col_button\">"
                        +"<div><input type=\"radio\" value=\"remove\" class=\"check_remove_id\" name=\"add_remove\">&nbsp;Remove</div>"
                        +"<div><input type=\"radio\" value=\"Add\" class=\"check_approve_id\" name=\"add_remove\" checked>&nbsp;Add</div>"
                        +"</td>"
                        +"</tr>"
                        +"<tr class=\"row\">"
                        +"<td class=\"col1\"></td>"
                        +"<td class=\"col_button\">"
                        +"<input type=\"submit\" value=\"Submit\" name=\"AddRemove\" class=\"form-content-submit\" >"
                        +"</td>"
                        +"</tr>"
                        +"</tbody></table>"
                        +"</form>"
                        +"</div>");
            }
            
            out.println("<br class=\"clear-all\">"
                    +"<div id=\"content-action\">"
                    +"<div id=\"header-tip\"></div>"
                    +"<form name=\"HanderContentComment\">"
                    +"<br class=\"clear-all\">"
                    +"<br class=\"clear-all\">"
                    +"<table id=\"content-action-table\" cellpadding=\"0\" cellspacing=\"0\">"
                    +"<thead>"
                    +"<tr>"
                    +"<th class=\"table-the-first\">#</th>"
                        +"<th class=\"table-1\">User</th>"
                        +"<th class=\"table-2\">Display Name</th>"
                        +"<th class=\"table-3\">User type</th>"
                        +"<th class=\"table-5\">Last Login</th>"
                    +"</tr>"
                    +"</thead>"
                    +"<tbody id=\"table-body\">");
                        show_user_info2(out, currUserType, currUserName);
            out.println("</tbody>"
                    +"</table>"
                    +"<br class=\"clear-all\">"
                    +"<br class=\"clear-all\">"
                    +"</form>"
                    +"</div>"
                    +"</div>"
                    +"</div>"
                    + "</div>"
                    );
        }finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (CorruptIndexException ex) {
            Logger.getLogger(add_user.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(add_user.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (CorruptIndexException ex) {
            Logger.getLogger(add_user.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(add_user.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
